package com.block.tool.service.impl;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.LocalDateTimeUtil;
import com.alibaba.fastjson.JSONObject;
import com.block.common.constant.AppRspCodeConstant;
import com.block.common.vo.ResponseVo;
import com.block.db.entity.ToolsNewsInfo;
import com.block.db.mapper.ToolsNewsInfoMapper;
import com.block.tool.model.vo.NewsInfoVO;
import com.block.tool.service.IToolsNewsInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 
 * @since 2024-07-24
 */
@Service
public class ToolsNewsInfoServiceImpl extends ServiceImpl<ToolsNewsInfoMapper, ToolsNewsInfo> implements IToolsNewsInfoService {

    @Override
    public ResponseVo getNewsList() {

        // 查询发布时间最新的50条数据
        List<ToolsNewsInfo> newsList = this.baseMapper.getLastNewsList();

        List<NewsInfoVO> resultList = new ArrayList<>();
        for (ToolsNewsInfo newsInfo : newsList) {

            NewsInfoVO newsInfoVO = new NewsInfoVO();
            newsInfoVO.setId(newsInfo.getId());
            newsInfoVO.setTitle(newsInfo.getTitleInfo());
            newsInfoVO.setAuthor(newsInfo.getAuthor());
            newsInfoVO.setDescInfo(newsInfo.getDescInfo());
            newsInfoVO.setPicUrl(newsInfo.getPicUrl());
            newsInfoVO.setContentUrl(newsInfo.getContentUrl());
            newsInfoVO.setPublishTime(LocalDateTimeUtil.format(newsInfo.getPublishTime(), DatePattern.NORM_DATETIME_MINUTE_PATTERN));

            resultList.add(newsInfoVO);
        }

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("dataList", resultList);

        return new ResponseVo(AppRspCodeConstant.SUCCESS, jsonObject);
    }
}
